package br.com.edoc.fabrica;

import java.util.ArrayList;
import java.util.List;
import org.aopalliance.aop.Advice;
import org.springframework.aop.Advisor;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.annotation.AnnotationMatchingPointcut;
import br.com.edoc.anotation.Consulta;
import br.com.edoc.anotation.SaveOrUpdate;
import br.com.edoc.aop.AdivisorConsultaAfter;
import br.com.edoc.aop.AdivisorConsultaBefore;
import br.com.edoc.aop.AdivisorSaveOrUpdateAfter;
import br.com.edoc.aop.AdivisorSaveOrUpdateBefore;
import br.com.edoc.aop.AdivisorSaveOrUpdateThrowException;

public class FabricaInterceptPoint {
	public List<Advisor> getInterceptPointConsulta() {
		List<Advisor> lista = new ArrayList<Advisor>();
		AnnotationMatchingPointcut pontoConsulta = getPointCut(Consulta.class);
		Advice consultaAfter = new AdivisorConsultaAfter();
		Advice consultaBefore = new AdivisorConsultaBefore();
		Advisor advisorConsultaAfter = new DefaultPointcutAdvisor(
				pontoConsulta, consultaAfter);
		Advisor advisorConsultaBefore = new DefaultPointcutAdvisor(
				pontoConsulta, consultaBefore);
		lista.add(advisorConsultaAfter);
		lista.add(advisorConsultaBefore);
		return lista;
	}

	public List<Advisor> getInterceptPointSaveOrUpdate() {
		List<Advisor> lista = new ArrayList<Advisor>();
		AnnotationMatchingPointcut pontoSaveOrUpdate = getPointCut(SaveOrUpdate.class);
		Advice saveOrUpdateAfter = new AdivisorSaveOrUpdateAfter();
		Advice saveOrUpdateBefore = new AdivisorSaveOrUpdateBefore();
		Advice saveOrUpdateThrow = new AdivisorSaveOrUpdateThrowException();
		Advisor advisorSaveOrUpdateAfter = new DefaultPointcutAdvisor(
				pontoSaveOrUpdate, saveOrUpdateAfter);
		Advisor advisorSaveOrUpdateBefore = new DefaultPointcutAdvisor(
				pontoSaveOrUpdate, saveOrUpdateBefore);
		Advisor advisorSaveOrUpdateThrow = new DefaultPointcutAdvisor(
				pontoSaveOrUpdate, saveOrUpdateThrow);
		lista.add(advisorSaveOrUpdateAfter);
		lista.add(advisorSaveOrUpdateBefore);
		lista.add(advisorSaveOrUpdateThrow);
		return lista;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	private AnnotationMatchingPointcut getPointCut(Class cls) {
		return new AnnotationMatchingPointcut(null, cls);
	}
}
